//
//  UIImage+Extensions.m
//  OneETrip
//
//  Created by zxy on 15/12/22.
//  Copyright © 2015年 traveksky. All rights reserved.
//

#import "UIImage+Extensions.h"

@implementation UIImage (Extensions)
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size {
    if (CGSizeEqualToSize(size, CGSizeZero)) {
        return nil;
    }
    
    if (!color) {
        return nil;
    }
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context,color.CGColor);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return img;
}
+ (UIImage *)specifiedStretchImageName:(NSString *)imageName {
    UIImage *image = [UIImage imageNamed:imageName];
    CGFloat top = image.size.height * 0.5;
    CGFloat left = image.size.width * 0.5;
    
    return [image resizableImageWithCapInsets:UIEdgeInsetsMake(top, left, top, left)
                                 resizingMode:UIImageResizingModeStretch];
}
- (UIImage *)specifiedStretchImage {
    CGFloat top = self.size.height * 0.5;
    CGFloat left = self.size.width * 0.5; 
    return [self resizableImageWithCapInsets:UIEdgeInsetsMake(top, left, top, left)
                                resizingMode:UIImageResizingModeStretch];
}
+ (UIImage *)smImageStrechableImage:(UIImage *)image {
    // 接近中间 去拉伸
    return [image resizableImageWithCapInsets:UIEdgeInsetsMake(image.size.height * 0.5f,
                                                               image.size.width * 0.6f - 1,
                                                               image.size.height * 0.5 - 1 ,
                                                               image.size.width * 0.4)
                                 resizingMode:UIImageResizingModeStretch];
}


+ (UIImage *)closeToTheEdgeToStretchImageStrechableImage:(UIImage *)image{
    
  
 
    
    // 接近边缘 去拉伸
    //image.size.height * 0.000001f
    //image.size.height * 0.000001f
    CGFloat top = image.size.height * 0.0000001f;
    CGFloat left = image.size.width * 0.0000001f;
   
    //  UIEdgeInsets insets = {top, left, bottom, right};
    return [image resizableImageWithCapInsets:UIEdgeInsetsMake(top,left,top,left)
                                 resizingMode:UIImageResizingModeStretch];
}

- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)newsize{
    // 创建一个bitmap的context
    // 并把它设置成为当前正在使用的context
    UIGraphicsBeginImageContext(newsize);
    // 绘制改变大小的图片
    [self drawInRect:CGRectMake(0, 0, newsize.width, newsize.height)];
    // 从当前context中创建一个改变大小后的图片
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    // 使当前的context出堆栈
    UIGraphicsEndImageContext();
    // 返回新的改变大小后的图片
    return scaledImage;
}

@end
